﻿#region 头注释
/******************************************************************************
文件名称    :   ChemistryCruor.cs
创建日期    :   2011/5/29 10:48:57
功能描述    :   ChemistryCruor实体类
修改日志    :   
    2011-05-29 10:48  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// ChemistryCruor实体类
    /// </summary>
    public class ChemistryCruor : EntityBase<ChemistryCruor, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ChemistryCruor()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            pt = null;
            aptt = null;
            tt = null;
            fib = null;
            fdp = null;
            ddimer = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override ChemistryCruor CreateNew()
        {
            return new ChemistryCruor();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.ChemistryCruor; }
        }

        /// <summary>
        /// 获取或设置Pt
        /// </summary>
        public decimal? Pt
        {
            get { return pt; }
            set
            {
                if (entityState == EntityState.Saved && pt != value) entityState = EntityState.Changed;
                pt = value;
            }
        }
        protected decimal? pt;

        /// <summary>
        /// 获取或设置Aptt
        /// </summary>
        public decimal? Aptt
        {
            get { return aptt; }
            set
            {
                if (entityState == EntityState.Saved && aptt != value) entityState = EntityState.Changed;
                aptt = value;
            }
        }
        protected decimal? aptt;

        /// <summary>
        /// 获取或设置Tt
        /// </summary>
        public decimal? Tt
        {
            get { return tt; }
            set
            {
                if (entityState == EntityState.Saved && tt != value) entityState = EntityState.Changed;
                tt = value;
            }
        }
        protected decimal? tt;

        /// <summary>
        /// 获取或设置Fib
        /// </summary>
        public decimal? Fib
        {
            get { return fib; }
            set
            {
                if (entityState == EntityState.Saved && fib != value) entityState = EntityState.Changed;
                fib = value;
            }
        }
        protected decimal? fib;

        /// <summary>
        /// 获取或设置Fdp
        /// </summary>
        public decimal? Fdp
        {
            get { return fdp; }
            set
            {
                if (entityState == EntityState.Saved && fdp != value) entityState = EntityState.Changed;
                fdp = value;
            }
        }
        protected decimal? fdp;

        /// <summary>
        /// 获取或设置D-二聚体
        /// </summary>
        public decimal? Ddimer
        {
            get { return ddimer; }
            set
            {
                if (entityState == EntityState.Saved && ddimer != value) entityState = EntityState.Changed;
                ddimer = value;
            }
        }
        protected decimal? ddimer;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override ChemistryCruor CopyTo(ChemistryCruor entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.pt = this.pt;
            entity.aptt = this.aptt;
            entity.tt = this.tt;
            entity.fib = this.fib;
            entity.fdp = this.fdp;
            entity.ddimer = this.ddimer;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<ChemistryCruor>
        {
            ChemistryCruor entity;
            public DefaultXmlSerializer(ChemistryCruor entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public ChemistryCruor Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Pt":
                        entity.pt = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Aptt":
                        entity.aptt = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Tt":
                        entity.tt = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Fib":
                        entity.fib = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Fdp":
                        entity.fdp = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Ddimer":
                        entity.ddimer = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Pt != null) writer.WriteElementString("Pt", XmlConvert.ToString(entity.Pt.Value));
                if (entity.Aptt != null) writer.WriteElementString("Aptt", XmlConvert.ToString(entity.Aptt.Value));
                if (entity.Tt != null) writer.WriteElementString("Tt", XmlConvert.ToString(entity.Tt.Value));
                if (entity.Fib != null) writer.WriteElementString("Fib", XmlConvert.ToString(entity.Fib.Value));
                if (entity.Fdp != null) writer.WriteElementString("Fdp", XmlConvert.ToString(entity.Fdp.Value));
                if (entity.Ddimer != null) writer.WriteElementString("Ddimer", XmlConvert.ToString(entity.Ddimer.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<ChemistryCruor>
        {
            ChemistryCruor entity;
            public DefaultJsonSerializer(ChemistryCruor entity)
                : base()
            {
                this.entity = entity;
            }

            public ChemistryCruor Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "pt":
                        entity.pt = DbConvert.ToNullableDecimal(propValue) ?? entity.pt;
                        break;
                    case "aptt":
                        entity.aptt = DbConvert.ToNullableDecimal(propValue) ?? entity.aptt;
                        break;
                    case "tt":
                        entity.tt = DbConvert.ToNullableDecimal(propValue) ?? entity.tt;
                        break;
                    case "fib":
                        entity.fib = DbConvert.ToNullableDecimal(propValue) ?? entity.fib;
                        break;
                    case "fdp":
                        entity.fdp = DbConvert.ToNullableDecimal(propValue) ?? entity.fdp;
                        break;
                    case "ddimer":
                        entity.ddimer = DbConvert.ToNullableDecimal(propValue) ?? entity.ddimer;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //Pt
                writer.WritePropertyName("pt");
                if (entity.Pt == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Pt.Value);
                }

                //Aptt
                writer.WritePropertyName("aptt");
                if (entity.Aptt == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Aptt.Value);
                }

                //Tt
                writer.WritePropertyName("tt");
                if (entity.Tt == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Tt.Value);
                }

                //Fib
                writer.WritePropertyName("fib");
                if (entity.Fib == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Fib.Value);
                }

                //Fdp
                writer.WritePropertyName("fdp");
                if (entity.Fdp == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Fdp.Value);
                }

                //D-二聚体
                writer.WritePropertyName("ddimer");
                if (entity.Ddimer == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Ddimer.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}